<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://unpkg.com/vuex@3.1.3/dist/vuex.js"></script>
    <script>
      var counter1 = {
        namespaced: true, //启用命名空间
        state: { count: 0 },
        mutations: {
          increase(state) {
            state.count++;
          },
        },
        actions: {
          increase: {
            root: true,//如果想在一个模块内定义的action, 在派发动作的时候不加前缀，就可以把它声明为root:true
            handler({ commit }) {
              commit("increase");
            },
          },
          changeName({commit}){
            //unknown local mutation type: changeName, global type: counter1/changeName
            //提交全局的mutation
            commit('changeName',null,{root:true});
          }
        },
      };
      var store = new Vuex.Store({
        state: { name: "A" }, //全局命名空间下的状态
        mutations: {
          //全局命名空间下的mutations
          changeName(state) {
            state.name = "B";
          },
        },
        modules: { counter1 },
      });
      //store.dispatch('counter1/changeName')
    </script>
  </body>
</html>
